package cn.edu.cdu.farmdeal.service.impl;

import cn.edu.cdu.farmdeal.domain.RoleDistribute;
import cn.edu.cdu.farmdeal.domain.UserInf;
import cn.edu.cdu.farmdeal.domain.UserInfExample;
import cn.edu.cdu.farmdeal.mapper.RoleDistributeMapper;
import cn.edu.cdu.farmdeal.mapper.UserInfMapper;
import cn.edu.cdu.farmdeal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.edu.cdu.farmdeal.column.RoleColumn.BUY_USER_ID;
import static cn.edu.cdu.farmdeal.column.UtilColumn.TRUE_INT;

/**
 * @Author: tupeng
 * @Date: 2020/3/12
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserInfMapper userInfMapper;
    @Autowired
    RoleDistributeMapper roleDistributeMapper;

    @Override
    public Boolean checkUser(String userName) {
        UserInf userInf = userInfMapper.getUserNameByName(userName);
        return userInf == null ? false : true;
    }

    @Override
    public int register(UserInf userInf) {
        userInfMapper.insertSelective(userInf);
        UserInfExample userInfExample = new UserInfExample();
        userInfExample.createCriteria().andUseridEqualTo(userInf.getUserid());
        List<UserInf> userInfList = userInfMapper.selectByExample(userInfExample);
        Integer userInfoId = userInfList.get(0).getUserInfoId();
        RoleDistribute roleDistribute=new RoleDistribute();
        roleDistribute.setRoleid(BUY_USER_ID);
        roleDistribute.setUserInfoId(userInfoId);
        //注册为采购人员
        roleDistributeMapper.insertSelective(roleDistribute);
        return TRUE_INT;
    }

    @Override
    public String getPassByName(UserInf userInf) throws IndexOutOfBoundsException{
        UserInfExample userInfExample = new UserInfExample();
        userInfExample.createCriteria().andUseridEqualTo(userInf.getUserid());
        List<UserInf> userInfList = userInfMapper.selectByExample(userInfExample);
        return userInfList.get(0).getPassword();
    }
}
